package com.niuke;

import org.junit.Test;

/**
 * @ClassName : coordinate
 * @Author : kele
 * @Date: 2023/3/12 15:50
 * @Description :
 */
public class coordinate {

    @Test
    public void demo1() {

        String s = "";

        String[] split = s.split(";");
        int heng = 0;
        int shu = 0;

        for (String s1 : split) {
            if (s1.matches("^[A|S|D|W][0-9]{1,2}")) {
                if (s1.startsWith("A")) {
                    int i = Integer.parseInt(s1.substring(1));
                    heng = heng + i;
                } else if (s1.startsWith("D")) {
                    int i = Integer.parseInt(s1.substring(1));
                    heng = heng - i;
                } else if (s1.startsWith("W")) {
                    int i = Integer.parseInt(s1.substring(1));
                    shu = shu - i;
                } else if (s1.startsWith("S")) {
                    int i = Integer.parseInt(s1.substring(1));
                    shu = shu - i;
                }

            }
        }

        System.out.println("" + heng + "," + shu);

    }

    @Test
    public void demo2() {

        String s = "x4%H0$ZL8@";
        int mode = 0;
        boolean repe = false;
        boolean num = false;
        char[] ch = s.toCharArray();

        if (s.length() > 8) {

            num = true;

            if (s.replaceAll("[A-Z]", "").length() < s.length()) {
                mode++;
            }
            if (s.replaceAll("[a-z]", "").length() < s.length()) {
                mode++;
            }

            if (s.replaceAll("[0-9]", "").length() < s.length()) {
                mode++;
            }

            if (s.replaceAll("\\D", "").length() < s.length()) {
                mode++;
            }

            for (int i = 0; i < ch.length - 3; i++) {
                int x = s.replace(s.substring(i, i + 3), "").length() + 3;
                if (s.replace(s.substring(i, i + 3), "").length() + 3 != s.length()) {
                    repe = true;
                    System.out.println("原长：" + s.length() + "     替换的长：" + x);
                    System.out.println("替换的部分：   " + s.substring(i, i + 3));
                    System.out.println("替换的字符串： " + s.replace(s.substring(i, i + 2), ""));
                    break;
                }
            }

        }

        if (mode >= 3 && !repe && num) {
            System.out.println(mode + "  " + repe + "   " + num);
            System.out.println("OK");
        } else {
            System.out.println(mode + "  " + repe + "   " + num);
            System.out.println("NG");
        }

    }

    @Test
    public void demo3() {
        String s = "qweq6879rasd3456asdffcx1234v234";

        String[] split = s.split("\\D+");
        for (String s1 : split) {
            System.out.println(s1);
            Math.max(1, 2);
        }
    }


    @Test
    public void demo4() {
//        String str = "";
//
//        String[] split = str.split(".");
//
//        for (String s : split) {
//            int num = Integer.parseInt(s);
//        }

//        System.out.println(get10("1010"));

        String[] split = "10.0.3.193".split("\\.");

        String ss = "";

        for (String s : split) {
            String res = get2(Integer.parseInt(s));
            ss = ss + res;
        }
        System.out.println(get10(ss));

    }

    public String get2(int num) {

        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            sb.append(num % 2);
            num = num / 2;
        }
        return String.format("%8s", sb.reverse().toString()).replace(" ", "0");
    }

    public long get10(String str) {

        StringBuilder sb = new StringBuilder(str);

        char[] chars = sb.reverse().toString().toCharArray();

        long num = 0;
        for (int i = 0; i < chars.length; i++) {
            num = (long) (num + (chars[i] - '0') * Math.pow(2, i));
        }
        return num;
    }


    @Test
    public void x() {
        System.out.println(ipToLong("10.0.3.193"));
    }

    public long ipToLong(String ipAddress) {
        String[] ipSegments = ipAddress.split("\\.");
        long result = 0;
        for (int i = 0; i < ipSegments.length; i++) {
            int segmentValue = Integer.parseInt(ipSegments[i]);
            result = result << 8 | segmentValue; // 通过位运算将4个字节合并为一个long类型整数
        }
        return result;
    }


    @Test
    public void demo5() {
        System.out.println(n1("10.0.3.193"));
        System.out.println(n2("167969729"));

    }

    public long n1(String ip) {
        String[] split = ip.split("\\.");

        long res = 0;

        for (String s : split) {
            int num = Integer.parseInt(s);
            res = res << 8 | num;
        }
        return res;
    }

    public String n2(String str) {

        String format = String.format("%32s", Long.toBinaryString(Long.parseLong(str))).replace(" ", "0");

        String[] bin = new String[4];

        String res = "";
        for (int i = 0; i < 4; i++) {
            int num = Integer.parseInt(format.substring(i * 8, (i + 1) * 8), 2);
            res = res + num + ".";
        }

        return res.substring(0,res.length()-1);
    }


}
